<template>
  <el-container>
    <el-header>
      <div class="report_header" ref="headerHeight">
        <div class="report_header_left"></div>
        <div style="width: 300px;display: flex;align-items: center;margin-left: 10px;">
          <svg-icon class="primary" :icon-class="$route.meta.icon"/>
          <span style="margin-left: 5px;" class="primary">{{$route.meta.title}}</span>
        </div>
        <div style="display: flex;flex-direction: row;justify-content: flex-end;width: 100%;">
          <div style="display: flex;flex-direction: row;flex-wrap: wrap;overflow: hidden;height:41px">

            <div class="report_header_query">
              <el-select style="width: 100%" v-model="query.status" clearable placeholder="请选择状态">
                <el-option label="全部" value=""></el-option>
                <el-option label="启用" value="1"></el-option>
                <el-option label="停用" value="0"></el-option>
              </el-select>
            </div>
            <div class="report_header_query">
              <el-input prefix-icon="el-icon-search" placeholder="岗位" v-model="query.name"></el-input>
            </div>
          </div>
          <el-button style="margin-left: 10px;margin-top: 5px;" size="small" type="primary" @click="queryClick">查询</el-button>
          <el-button style="margin-left: 10px;margin-top: 5px;" size="small" type="primary" @click="addClick"
                     v-permission="['sysPost.add']">新增</el-button>
        </div>
      </div>
<!--      <div>-->
<!--        <svg-icon class="primary" :icon-class="$route.meta.icon"/>-->
<!--        <span class="primary">{{$route.meta.title}}</span>-->
<!--      </div>-->
<!--      <div class="header-button-container">-->
<!--        <el-button :icon="queryType? 'el-icon-search':'el-icon-close'" type="primary"-->
<!--                   @click="chooseClick">{{queryType == false?'隐藏':'查询'}}-->
<!--        </el-button>-->
<!--        <el-button v-permission="['sysPost.add']" icon="el-icon-plus" type="info" style="margin-left: 10px" @click="addClick">新增-->
<!--        </el-button>-->
<!--      </div>-->
    </el-header>
    <div class="pd10">
<!--      <div style="margin-bottom: 10px;display: flex;align-items: center"  v-show="queryType == false">-->
<!--        <el-input prefix-icon="el-icon-search" placeholder="岗位" style="width: 200px" v-model="query.name"></el-input>-->
<!--        <span style="margin-left: 10px;font-size: 12px;color: #909399;">状态</span>-->
<!--        <el-select style="margin-left: 5px" v-model="query.status" placeholder="请选择状态">-->
<!--          <el-option label="全部" value=""></el-option>-->
<!--          <el-option label="启用" value="1"></el-option>-->
<!--          <el-option label="停用" value="0"></el-option>-->
<!--        </el-select>-->
<!--        <el-button style="margin-left: 10px"  icon="el-icon-search" type="primary" @click="queryClick">查询-->
<!--        </el-button>-->
<!--        <el-button style="margin-left: 10px"  icon="el-icon-refresh-right" type="info" @click="resetClick">重置-->
<!--        </el-button>-->
<!--      </div>-->
      <el-table
        :data="sysPostData"
        border
        stripe
        v-loadMore="[payLoads,'sysPost-list']"
        :height="curHeight"
        style="width: 100%">
        <el-table-column
          type="index"
          width="50"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="岗位名称"
          width="180"
          align="center"
          prop="name"
        >
        </el-table-column>
        <el-table-column
          label="状态"
          width="80"
          align="center"
        >
          <template slot-scope="scope">
            <el-tag :type="scope.row.status == 0?'info':'success'">
              <span>{{scope.row.status | watchStatus}}</span>
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="岗位职责"
          align="left"
          prop="remark"
        >
        </el-table-column>
        <el-table-column
          label="功能"
          align="center"
          prop="remark"
          width="80"
          fixed="right"
        >
        <template slot-scope="scope">
          <el-tooltip v-permission="['sysPost.edit']" class="item" effect="dark" content="启用或停用" placement="top-start">
            <el-button @click="openSysPost(scope.row)" :loading="scope.row.loading" type="primary" size="mini">{{scope.row.status == 0?'启用':'停用'}}</el-button>
          </el-tooltip>
        </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="80">
          <template slot-scope="scope">
            <div>
              <el-tooltip v-permission="['sysPost.edit']" class="item" effect="dark" content="编辑" placement="top-start">
                <a  @click="editClick(scope.row)">
                  <i style="color: #409EFF" class="el-icon-edit"></i>
                </a>
              </el-tooltip>
              <el-tooltip v-permission="['sysPost.del']" class="item" effect="dark" content="删除" placement="top-start">
                <a style="margin-left: 10px"  @click="deleteSysPost(scope.row)">
                  <i style="color: #F56C6C" class="el-icon-delete"></i>
                </a>
              </el-tooltip>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-footer class="el-footerBox">
      <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize" @pagination="loadSysPost"></pagination>
    </el-footer>
  </el-container>
</template>

<script>
  import loadMore from '@/utils/loadMore.js'
  import permission from '@/directive/permission/index.js' // 权限判断指令
  import {list, del, update} from '@/api/sysPost'
  import utils from '../../utils/utils.js'
  import {EventBus} from '@/api/eventBus'
  import Pagination from '@/components/Pagination';
  export default {
    name:'sysPost-list',
    directives: { permission,loadMore },
    activated() {
      this.payLoads = new Date().getTime();
    },
    data() {
      return {
        payLoads:'',
        sysPostData:[],
        pageNum: 1,
        pageSize: 20,
        total: 0,
        queryType: true,
        query: {
          name: '',
          status: ''
        },
        keyword:{
          name: '',
          status: ''
        },
        sysPostOpenForm: {
          id: '',
          status: '',
        },
        curHeight:null
      }
    },
    components: { Pagination },
    created() {
      var _this = this
      this.loadSysPost()
      EventBus.$on('sysPost-add', res => { // 监听新增完毕
        // 更新列表数组
        _this.sysPostData.splice(0, 0,res)
      });
      EventBus.$on('sysPost-edit', res => { // 监听新增完毕
        // 更新列表数组
        for (var i=0;i<_this.sysPostData.length;i++){
           if (_this.sysPostData[i].id == res.id){
             _this.sysPostData[i] = res
           }
        }
      })
    },
    filters: {
      watchStatus(val) {
        if (val == 1) {
          return '启用'
        } else {
          return '停用'
        }
      }
    },
    mounted(){
      this.comeIn()
      this.beforeMount()
    },
    methods: {
      beforeMount() {
        var _this = this
        window.onresize = () => {
          return (() => {
            if (this.queryType == false) {
              var h = document.documentElement.clientHeight || document.body.clientHeight;
              _this.curHeight = h - 266//减去页面上固定高度height
            } else {
              var h = document.documentElement.clientHeight || document.body.clientHeight;
              _this.curHeight = h - 220//减去页面上固定高度height
            }
          })();
        };
      },
      comeIn(){
        console.log('222')
        var h = document.documentElement.clientHeight || document.body.clientHeight;
        this.curHeight = h - 220//减去页面上固定高度height
      },
      editClick(row) {
        this.$router.push({
          path: '/sysPost/edit', query: {id: row.id}
        })
      },
      openSysPost(row) {
        var _this = this
        row.loading = true
        console.log(row);
        this.sysPostOpenForm.id = row.id;
        if (row.status == 0) {
          this.sysPostOpenForm.status = 1
        } else if (row.status == 1) {
          this.sysPostOpenForm.status = 0
        }
        update(_this.sysPostOpenForm).then(response => {
          console.log(response)
          _this.sysPostData.forEach(function (item) {
             if (item.id == row.id){
               item.status = response.data.status
             }
          })
          _this.$message({
            message: response.content,
            offset: 120,
            center: true
          });
          row.loading = false
        }).catch(error => {
          row.loading = false
          console.log('error!!')
        });
      },
      chooseClick() {
        this.queryType = !this.queryType
        if (this.queryType == false) {
          var h = document.documentElement.clientHeight || document.body.clientHeight;
          this.curHeight = h - 266//减去页面上固定高度height
        } else {
          var h = document.documentElement.clientHeight || document.body.clientHeight;
          this.curHeight = h - 220//减去页面上固定高度height
        }
      },
      addClick() {
        this.$router.push({
          path: '/sysPost/add'
        })
      },
      deleteSysPost(data) {
        console.log(data)
        var _this = this
        this.$confirm('确认删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          del(data.id).then(response => {
            console.log(response)
            _this.sysPostData.forEach(function (item,index) {
              if (item.id == data.id){
                _this.sysPostData.splice(index,1)
              }
            });
            _this.$message({
              message: response.content,
              offset: 120,
              center: true
            });
          }).catch(error => {
            console.log('error!!')
          });
        }).catch(() => {
        });
      },
      queryClick() {
        this.keyword = this.query;
        this.pageNum = 1
        this.loadSysPost()
      },
      resetClick() {
        this.query.name = '';
        this.query.status = '';
        this.keyword = this.query;
        this.pageNum = 1;
        this.loadSysPost()
      },
      loadSysPost() {
        var _this = this
        let params = new URLSearchParams();
        params.append('pageNum', _this.pageNum);
        params.append('pageSize', _this.pageSize);
        params.append('status', _this.keyword.status);
        params.append('name', _this.keyword.name);
        list(params).then(response => {
          console.log(response)
          response.data.data.map(item => { return item.loading = false })
          _this.sysPostData = response.data.data
          _this.total = response.data.total
        }).catch(error => {
          console.log('error!!')
        });
      },
    }
  }
</script>

<style scoped>
  .el-footerBox {
    height: 50px !important;
    line-height: 50px !important;
    border-top: 1px solid #eeeeee;
  }
  .app {
    background-color: rgba(248, 248, 248, 0.8);
  }

  /deep/.el-header {
    padding: 10px;
    /* height: 100% !important; */
    padding-bottom: 0;
  }

  .report_header {
    width: 100%;
    min-height: 40px;
    box-sizing: border-box;
    background-color: rgba(248, 248, 248, 0.8);
    display: flex;
    align-items: center;
    padding-bottom: 5px;
    position: relative;
    padding-left: 5px;
    padding-right: 5px;
  }
  .report_header_left {
    width: 5px;
    height: 100%;
    background-color: #409EFF;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
  }
  .report_header_date {
    margin-left: 10px;
    width: 250px;
    margin-top: 5px;
  }
  .report_header_query {
    margin-left: 10px;
    width: 180px;
    margin-top: 5px;
  }
</style>
